package com.park.user.service.order.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.park.dao.mapper.order.ParkOrderInfoMapper;
import com.park.dao.mapper.user.UserMapper;
import com.park.entity.order.MerOrderStartEndDto;
import com.park.entity.order.ParkOrderInfo;
import com.park.entity.page.PageInfoParm;
import com.park.entity.user.UserInfo;
import com.park.entity.user.UserLoginVo;
import com.park.user.service.order.ParkOrderInfoService;
import com.park.utils.wx.WxUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Administrator
 * @title: ParkOrderInfoServiceImpl
 * @projectName park
 * @description: 用户订单
 * @date 2020/5/412:17
 */
@Service(value = "parkOrderInfoService")
public class ParkOrderInfoServiceImpl implements ParkOrderInfoService {

    @Autowired
    private ParkOrderInfoMapper parkOrderInfoMapper;

    @Autowired
    private UserMapper userMapper;

    private static Lock lock = new ReentrantLock();

    /**
     *  查询我的订单
     * @param pageInfoParm
     * @return
     */
    @Override
    public PageInfo myOrderList(PageInfoParm<UserLoginVo> pageInfoParm,Integer userId) {
        PageHelper.startPage(pageInfoParm.getPageNum(),pageInfoParm.getPageSize());
        List<ParkOrderInfo> parkOrderInfos = parkOrderInfoMapper.myOrderList(userId);
        PageInfo<ParkOrderInfo> pageInfo = new PageInfo<ParkOrderInfo>(parkOrderInfos);
        return pageInfo;
    }
    /**
     　　* @description:  用户支付订单
     　　* @param [userId 用户id, orderStartEndDto]
     　　* @return
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/6 14:45
     　　*/
    @Override
    public Map<String,String> pay(Integer userId, MerOrderStartEndDto orderStartEndDto,String ipAddr) {
        lock.lock();
        try {
            Integer orderId = orderStartEndDto.getOrderId();
            ParkOrderInfo parkOrderInfo = parkOrderInfoMapper.selectByPrimaryKey(orderId);
            UserInfo userInfo = userMapper.selectByPrimaryKey(userId);
            //查询订单是否已经结束
            String body = "滨和集团-泊车";
            /* String orderNo = parkOrderInfo.getOrderNo();*/
            String orderNo = IdUtil.simpleUUID();
            //查询车牌号码是否在支付,乐观锁解决同时支付一个订单
            BigDecimal totalFee = parkOrderInfo.getOrderTotal();
            String ip = ipAddr;
            Map<String, String> unifiedorder = WxUtils.unifiedorder(body, orderNo, totalFee, ip,userInfo.getOpenId());
            //更新操作
            parkOrderInfo.setPayUserId(userId);
            parkOrderInfo.setPayPhoneNumber(userInfo.getPhoneNumber());
            parkOrderInfo.setOrderNo(orderNo);
            parkOrderInfo.setTimeStamp(System.currentTimeMillis());
            parkOrderInfoMapper.updateByPrimaryKeySelective(parkOrderInfo);
            return unifiedorder;
        }finally {
            lock.unlock();
        }
    }
}
